package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.model.entity.SystemPost;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.mapper.SystemPostMapper;
import com.atguigu.lease.web.admin.service.SystemPostService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【system_post(岗位信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemPostServiceImpl extends ServiceImpl<SystemPostMapper, SystemPost>
        implements SystemPostService {

    @Autowired
    private SystemUserService systemUserService;

    /**
     * 分页获取岗位信息
     *
     * @param current 当前页
     * @param size    每页数据
     * @return 岗位信息
     */
    @Override
    public IPage<SystemPost> getPostPage(long current, long size) {
        IPage<SystemPost> list = baseMapper.selectPage(new Page<>(current, size), null);
        return list;
    }

    @Override
    public void removePostById(Long id) {
        LambdaQueryWrapper<SystemUser> systemUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        systemUserLambdaQueryWrapper.eq(SystemUser::getPostId, id);
        long count = systemUserService.count(systemUserLambdaQueryWrapper);
        if (count > 0) {
            throw new LeaseException("该岗位下还有用户，请处理后再次尝试删除", 210);
        }
        baseMapper.deleteById(id);
    }

    @Override
    public void updatePostStatus(Long id, BaseStatus status) {
        LambdaUpdateWrapper<SystemPost> systemPostLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        systemPostLambdaUpdateWrapper.eq(SystemPost::getId, id);
        systemPostLambdaUpdateWrapper.set(SystemPost::getStatus, status);
        baseMapper.update(systemPostLambdaUpdateWrapper);
    }
}




